﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DTO;
using BUS;
using moved;


namespace WebChamCong
{
    public partial class IndexForm : System.Web.UI.Page
    {
        private DateTime _currentDate;

        protected void Page_Load(object sender, EventArgs e)
        {
            this._currentDate = DateTime.Now;
            
            if (!this.IsPostBack)
            {
                // Load month dropdownlist
                LoadMonths(this._currentDate);

                // Load years dropdownlist
                LoadYears(this._currentDate);

                // Default load list users with default
                LoadUsers(this._currentDate);
            }
        
        }

        private void LoadMonths(DateTime currentDate)
        {
            DateTime localTime = currentDate;

            this.ddl_month.Items.Clear();

            for (int i = 1; i < 13; i++)
            {
                this.ddl_month.Items.Add(new ListItem("Tháng "+i, i.ToString()));   
       
                // Check if month 's current day has value
                if (localTime.Month == i)
                {
                    this.ddl_month.SelectedIndex = i-1;
                }

            }

        }

        private void LoadYears(DateTime currentDate)
        {
            DateTime localTime = currentDate;

            this.ddl_year.Items.Clear();

            for (int i = 2000; i < 2051; i++)
            {
                this.ddl_year.Items.Add(new ListItem("Năm "+i, i.ToString()));

                // Check if year 's current day has value
                if (localTime.Year == i)
                {
                    this.ddl_year.SelectedIndex = i - 2000;
                }

            }
        }

        private void LoadUsers(DateTime currentDate)
        {
            DateTime localTime = currentDate;

            DataTable listUsers = new DataTable();

            listUsers.Columns.Add("STT", System.Type.GetType("System.String"));
            listUsers.Columns.Add("Ho Ten", System.Type.GetType("System.String"));

            for (int i = 1; i < DateTime.DaysInMonth(localTime.Year, localTime.Month)+1; i++)
            {
                listUsers.Columns.Add(i.ToString(), System.Type.GetType("System.String"));    
            }            

            DataRow newRow = listUsers.NewRow();
            newRow["STT"] = "1";
            newRow["Ho Ten"] = "Nguyen Huu Phuc";            

            for (int i = 1; i < DateTime.DaysInMonth(localTime.Year, localTime.Month)+1; i++)
            {
                newRow[i.ToString()] = "H";
            }
           
            listUsers.Rows.Add(newRow);

            listUsers.AcceptChanges();

            this.gv_listUsers.DataSource = listUsers;
            this.gv_listUsers.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddl_month_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get current value in ddl_month
            int currentMonth = Int32.Parse(this.ddl_month.Items[this.ddl_month.SelectedIndex].Value.ToString());

            // Get current value in ddl_year
            int currentYear = Int32.Parse(this.ddl_year.Items[this.ddl_year.SelectedIndex].Value.ToString());

            int currentDay = 1;

            DateTime currentDate = new DateTime(currentYear, currentMonth, currentDay);

            // Rebind ddl_month
            this.LoadMonths(currentDate);

            // Rebind ddl_year
            this.LoadYears(currentDate);

            // Rebind gv_users
            this.LoadUsers(currentDate);
        }

        protected void ddl_year_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
